package com.jjj.springmvc.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@ComponentScan(basePackages= {"com.jjj.springmvc"},excludeFilters= {@Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)})
@PropertySource(value="classpath:db.properties")
public class RootConfig {
	
	@Autowired
	private Environment environment;
	
	@Bean(name = "dataSource")
    public DruidDataSource getDataSource() {
        try {
        	DruidDataSource dataSource = new DruidDataSource();
        	dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
            dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
            dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
            dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
            return dataSource;
        } catch (Exception e) {
        	e.printStackTrace();
            return null;
        }
    }
	@Bean(name = "dataSource1")
	public DruidDataSource getDataSource1() {
		try {
			DruidDataSource dataSource = new DruidDataSource();
			dataSource.setUrl("jdbc:mysql://localhost:3306/mysql");
			dataSource.setDriverClassName("com.mysql.jdbc.Driver");
			dataSource.setUsername("root");
			dataSource.setPassword("root");
			return dataSource;
		} catch (Exception e) {
			return null;
		}
	}
}
